package fr.inserm.u674.chcdbweb.model.forms;

import javax.servlet.http.HttpServletRequest;

import fr.inserm.u674.chcdbweb.model.DAO.DAOException;
import fr.inserm.u674.chcdbweb.model.DAO.TissueDAO;
import fr.inserm.u674.chcdbweb.model.DAO.TissueExtractionDNADAO;
import fr.inserm.u674.chcdbweb.model.DAO.TissueExtractionRNADAO;
import fr.inserm.u674.chcdbweb.model.DTO.Tissue;
import fr.inserm.u674.chcdbweb.model.DTO.TissueExtraction;

public class DisplayExtractionForm extends Form {

	// Constants ---------------------------------------------------------------------------------

	private static final String FIELD_CHCID = "chcID";
	private static final String FIELD_TISSUETYPEID = "tissueTypeID";
	private static final String FIELD_RNA_EXTRACTION = "rnaExtractionCheckbox";
	private static final String FIELD_DNA_EXTRACTION = "dnaExtractionCheckbox";

	// VARS

	private TissueDAO tissueDAO;
	public DisplayExtractionForm(TissueDAO tissueDAO, TissueExtractionDNADAO dnaExtractionDAO,
		TissueExtractionRNADAO rnaExtractionDAO) {
		this.tissueDAO = tissueDAO;
	}

	public TissueExtraction displayExtraction(HttpServletRequest request) {
		TissueExtraction extraction = new TissueExtraction();
		Tissue tissue = new Tissue();
		extraction.setTissueManipulated(tissue);

		processExtractionType(request, extraction);

		return extraction;
	}

	// Field processors ---------------------------------------------------------------------------

	/**
	 * Retrieves the chcID contained in the HTTP request, checks if it is valid, and sets it to the
	 * chcID field of the Tissue object
	 * 
	 * @param request
	 * @param tissue
	 * @throws DAOException
	 */
	public void processCHCID(HttpServletRequest request, TissueExtraction extraction) throws DAOException {
		String chcID = FormUtil.getFieldValue(request, FIELD_CHCID);
		Integer chcIDtemp = null;
		if (chcID != null) {
			try {
				chcIDtemp = validateCHCID(chcID);
			}
			catch (ValidatorException e) {
				setError(FIELD_CHCID, e.getMessage());
			}
			extraction.getTissueManipulated().setChcID(chcIDtemp);
		}
	}

	/**
	 * Retrieves the tissueTypeID contained in the HTTP request, checks if it is valid, and sets it
	 * to the tissueTypeID field of the Tissue object
	 * 
	 * @param request
	 * @param tissue
	 * @throws DAOException
	 */
	public void processTissueTypeID(HttpServletRequest request, TissueExtraction extraction)
		throws DAOException {
		String tissueTypeID = FormUtil.getFieldValue(request, FIELD_TISSUETYPEID);
		if (tissueTypeID != null) {
			try {
				validateTissueTypeID(tissueTypeID);
			}
			catch (ValidatorException e) {
				setError(FIELD_TISSUETYPEID, e.getMessage());
			}
			extraction.getTissueManipulated().setTissueTypeID(tissueTypeID);
		}
	}

	/**
	 * Retrieves the two extractions checkboxex field values from the HTTP request, checks if at
	 * least one these is selected
	 * 
	 * @param request
	 * @param extraction
	 */
	public void processExtractionType(HttpServletRequest request, TissueExtraction extraction) {

		String[] extractionCheckboxes = new String[2];
		String rnaExtractionCheckbox = FormUtil.getFieldValue(request, FIELD_RNA_EXTRACTION);
		String dnaExtractionCheckbox = FormUtil.getFieldValue(request, FIELD_DNA_EXTRACTION);
		extractionCheckboxes[0] = dnaExtractionCheckbox;
		extractionCheckboxes[1] = rnaExtractionCheckbox;
		try {
			validateSelectedExtraction(extractionCheckboxes);

		}
		catch (ValidatorException e) {
			setError(FIELD_DNA_EXTRACTION, e.getMessage());
		}

	}

	// Field validators ---------------------------------------------------------------------------

	public Integer validateCHCID(String putativeCHCID) throws ValidatorException, DAOException {
		Integer chcID = null;
		if (FormUtil.isEmpty(putativeCHCID))
			throw new ValidatorException("The CHC ID cannot be null.");
		if (putativeCHCID != null) {
			try {
				chcID = new Integer(putativeCHCID);
				if (chcID < 0) {
					throw new ValidatorException("a CHC ID cannot be negative.");
				}
				else if (!tissueDAO.existsCHCID(chcID)) {
					throw new ValidatorException("This CHC ID does not exist in the database.");
				}
			}
			catch (NumberFormatException e) {
				throw new ValidatorException("Please enter a valid Number.");
			}
		}
		return chcID;
	}

	public void validateTissueTypeID(String tissueTypeID) throws ValidatorException, DAOException {
		if (FormUtil.isEmpty(tissueTypeID))
			throw new ValidatorException("The Tissue Type ID cannot be null.");
		if (tissueTypeID != null) {
			if (tissueTypeID.length() > 5) {
				throw new ValidatorException("A Tissue Type ID cannot be longer than 5 characters.");
			}
			else if (!tissueDAO.existsTissueTypeID(tissueTypeID)) {
				throw new ValidatorException("This Tissue Type ID does not exist in the database.");
			}
		}
	}

	public void validateSelectedExtraction(String[] extractionCheckboxes) throws ValidatorException {

		if (FormUtil.isCheckboxChecked(extractionCheckboxes[0]) == false
			&& FormUtil.isCheckboxChecked(extractionCheckboxes[1]) == false) {
			throw new ValidatorException("At least one type of extraction must be selected.");
		}
	}

}
